package com.yunpeng.aidevice.dao.constant;

import com.yunpeng.aidevice.dao.mapper.record.MerchantNoticeMailMapper;
import com.yunpeng.aidevice.dao.model.record.MerchantNoticeMailDO;
import com.yunpeng.aidevice.dao.service.IDictionariesService;
import com.yunpeng.common.util.AppConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DictionariesConstant 字典常量
 *
 * @author : Moore(hemw)
 * @date : 2020-04-18
 */
public enum DictionariesConstant {

    /**
     * 实例对象
     */
    INSTANCE;


    /**
     * 服务器序号集合
     */
    private List<String> serverMachineNumberList;

    /**
     * 系统默认用户
     */
    private String defaultUserCode;

    /**
     * 用户登录默认角色
     */
    private String defaultRoleName;

    /**
     * 系统运维管理员
     */
    private String adminRoleName;

    /**
     * 业务运营管理员
     */
    private String operateAdminRoleName;

    /**
     * 系统默认租户代码
     */
    private String defaultTenantCode;
    /**
     * 设备通知规则名
     */
    private String systemDeviceNoticeRuleName;
    /**
     * 设备通知参数名
     */
    private String systemDeviceNoticeParameterName;

    /**
     * 系统默认设备报警值
     */
    private Double systemDefaultNoticeTemp;

    /**
     * 德沃数据上传路径
     */
    private String dwUploadDataApi;

    /**
     * 允许文件上传后缀
     */
    private String fileAllowSuffix;

    /**
     * 最大文件大小
     */
    private Long maxFileSize;

    /**
     * 文件保存路径
     */
    private String fileSavePath;

    /**
     * 文件访问url
     */
    private String fileAccessUrl;

    /**
     * 默认邮箱发送配置
     */
    private Map<String, MerchantNoticeMailDO> defaultNoticeMailConfigMap;

    /**
     * 商户邮箱发送配置
     */
    private Map<String, MerchantNoticeMailDO> merchantNoticeMailConfigMap;

    /**
     * 字典服务
     */
    private IDictionariesService dictionariesService;

    private MerchantNoticeMailMapper merchantNoticeMailMapper;


    /**
     * 获取字典服务
     *
     * @return
     */
    public IDictionariesService getDictionariesService() {
        if (dictionariesService == null) {
            dictionariesService = AppConfig.getBean(IDictionariesService.class);
        }
        return dictionariesService;
    }

    public MerchantNoticeMailMapper getMerchantNoticeMailMapper() {
        if (merchantNoticeMailMapper == null) {
            merchantNoticeMailMapper = AppConfig.getBean(MerchantNoticeMailMapper.class);
        }
        return merchantNoticeMailMapper;
    }

    /**
     * 清理本地缓存（更新字典后使用）
     */
    public void clearCache() {
        this.serverMachineNumberList = null;
        this.defaultUserCode = null;
        this.defaultRoleName = null;
        this.adminRoleName = null;
        this.operateAdminRoleName = null;
        this.defaultTenantCode = null;
        this.systemDeviceNoticeRuleName = null;
        this.systemDeviceNoticeParameterName = null;
        this.systemDefaultNoticeTemp = null;
        this.dwUploadDataApi = null;
        this.fileAllowSuffix = null;
        this.maxFileSize = null;
        this.fileSavePath = null;
        this.fileAccessUrl = null;
    }

    /**
     * 获取服务器机器序号集合
     *
     * @return
     */
    public List<String> getServerMachineNumberList() {
        if (serverMachineNumberList == null) {
            serverMachineNumberList = getDictionariesService().getServerMachineNumberList();
        }
        return serverMachineNumberList;
    }


    public String getDefaultUserCode() {
        if (defaultUserCode == null) {
            defaultUserCode = getDictionariesService().getDefaultUserCode();
        }
        return defaultUserCode;
    }

    public String getDefaultRoleName() {
        if (defaultRoleName == null) {
            defaultRoleName = getDictionariesService().getSystemDefaultRole();
        }
        return defaultRoleName;
    }

    public String getAdminRoleName() {
        if (adminRoleName == null) {
            adminRoleName = getDictionariesService().getSystemAdminRole();
        }
        return adminRoleName;
    }

    public String getOperateAdminRoleName() {
        if (operateAdminRoleName == null) {
            operateAdminRoleName = getDictionariesService().getSystemOperateAdminRole();
        }
        return operateAdminRoleName;
    }

    /**
     * 获取默认租户代码
     * TODO 后续多租户时，此处需要改造
     *
     * @return
     */
    public String getDefaultTenantCode() {
        if (defaultTenantCode == null) {
            defaultTenantCode = getDictionariesService().getSystemDefaultTenantCode();
        }
        return defaultTenantCode;
    }


    public String getSystemDeviceNoticeRuleName() {
        if (systemDeviceNoticeRuleName == null) {
            systemDeviceNoticeRuleName = getDictionariesService().getSystemDeviceNoticeRuleName();
        }
        return systemDeviceNoticeRuleName;
    }

    public String getSystemDeviceNoticeParameterName() {
        if (systemDeviceNoticeParameterName == null) {
            systemDeviceNoticeParameterName = getDictionariesService().getSystemDeviceNoticeParameterName();
        }
        return systemDeviceNoticeParameterName;
    }

    public Double getSystemDefaultNoticeTemp() {
        if (systemDefaultNoticeTemp == null) {
            systemDefaultNoticeTemp = getDictionariesService().getSystemDefaultNoticeTemp();
        }
        return systemDefaultNoticeTemp;
    }

    public String getDwUploadDataApi() {
        if (dwUploadDataApi == null) {
            dwUploadDataApi = getDictionariesService().getDwUploadDataApi();
        }
        return dwUploadDataApi;
    }


    public String getFileAllowSuffix() {
        if (fileAllowSuffix == null) {
            fileAllowSuffix = getDictionariesService().getFileAllowSuffix();
        }
        return fileAllowSuffix;
    }

    public Long getMaxFileSize() {
        if (maxFileSize == null) {
            maxFileSize = getDictionariesService().getMaxFileSize() * 1024L * 1024L;
        }
        return maxFileSize;
    }

    public String getFileSavePath() {
        if (fileSavePath == null) {
            fileSavePath = getDictionariesService().getFileSavePath();
        }
        return fileSavePath;
    }

    public String getFileAccessUrl() {
        if (fileAccessUrl == null) {
            fileAccessUrl = getDictionariesService().getFileAccessUrl();
        }
        return fileAccessUrl;
    }

    public MerchantNoticeMailDO getDefaultNoticeMailConfig(String tenantCode) {
        if (defaultNoticeMailConfigMap == null) {
            defaultNoticeMailConfigMap = new HashMap<>(16);
        }
        if (defaultNoticeMailConfigMap.containsKey(tenantCode)) {
            return defaultNoticeMailConfigMap.get(tenantCode);
        } else {
            MerchantNoticeMailDO merchantNoticeMailDO = getMerchantNoticeMailMapper().getDefaultMailConfig(tenantCode);
            if (merchantNoticeMailDO == null) {
                return getDefaultNoticeMailConfig(tenantCode);
            }
            defaultNoticeMailConfigMap.put(tenantCode, merchantNoticeMailDO);
            return merchantNoticeMailDO;
        }
    }

    public MerchantNoticeMailDO getMerchantNoticeMailConfig(String merchantCode, String tenantCode) {
        if (merchantNoticeMailConfigMap == null) {
            merchantNoticeMailConfigMap = new HashMap<>(16);
        }
        if (merchantNoticeMailConfigMap.containsKey(merchantCode)) {
            return merchantNoticeMailConfigMap.get(merchantCode);
        } else {
            MerchantNoticeMailDO merchantNoticeMailDO = getMerchantNoticeMailMapper().getMerchantMailConfig(merchantCode, tenantCode);
            if (merchantNoticeMailDO == null) {
                return getDefaultNoticeMailConfig(tenantCode);
            }
            merchantNoticeMailConfigMap.put(merchantCode, merchantNoticeMailDO);
            return merchantNoticeMailDO;
        }
    }

    public void updateCacheNoticeMailConfig(MerchantNoticeMailDO merchantNoticeMailDO) {
        if (merchantNoticeMailDO == null) {
            return;
        }
        if (merchantNoticeMailDO.getIsDefault()) {
            if (defaultNoticeMailConfigMap == null) {
                defaultNoticeMailConfigMap = new HashMap<>(16);
            }
            defaultNoticeMailConfigMap.put(merchantNoticeMailDO.getTenantCode(), merchantNoticeMailDO);
        } else {
            if (merchantNoticeMailConfigMap == null) {
                merchantNoticeMailConfigMap = new HashMap<>(16);
            }
            merchantNoticeMailConfigMap.put(merchantNoticeMailDO.getMerchantCode(), merchantNoticeMailDO);
        }
    }

}
